package main;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Projections;
import main.database.DatabaseMan;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Filters.eq;

public class CatlogHelper {
    public static String typeToString(String type) {
        String result = "综合前100";
        switch (type) {
            case "qihuan":
                result = "奇幻";
                break;
            case "xuanhuan":
                result = "玄幻";
                break;
            case "wuxia":
                result = "武侠";
                break;
            case "xianxia":
                result = "仙侠";
                break;
            case "dushi":
                result = "都市";
                break;
            case "yanqing":
                result = "言情";
                break;
            case "lishi":
                result = "历史";
                break;
            case "junshi":
                result = "军事";
                break;
            case "youxi":
                result = "游戏";
                break;
            case "tiyu":
                result = "体育";
                break;
            case "kehuan":
                result = "科幻";
                break;
            case "lingyi":
                result = "灵异";
                break;
        }
        return result;
    }

    public static List<String[]> getCatlogInfos(int type) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfRootCatlog);
        List<Document> documents = collection.find().projection(Projections.include("uri", "name")).into(new ArrayList<Document>());
        List<String[]> resultList = new ArrayList<>();

        //加上综合
        String[] info = new String[2];
        info[0] = "综合前100";
        if (type == 0) {// for top page
            info[1] = "/toppage" + "/zonghe";
        }
        else {
            info[1] = "/rankpage" + "/zonghe" + "/0";
        }
        resultList.add(info);
        for (int i = 0; i < documents.size(); ++i) {
            info = new String[2];
            info[0] = documents.get(i).get("name").toString();
            if (type == 0) {// for top page
                info[1] = "/toppage" + documents.get(i).get("uri").toString();
            }
            else {
                info[1] = "/rankpage" + documents.get(i).get("uri").toString() + "/0";
            }
            resultList.add(info);
        }

        return resultList;
    }

    public static long countOfRootCatlog() {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfRootCatlog);
        return collection.countDocuments();
    }

    public static Bson typeToFilter(String type) {
        Bson filter;
        switch (type) {
            case "zonghe":
            default:
                filter = new Document();
                break;
            case "qihuan":
                filter = eq("rootcatlog", "奇幻");
                break;
            case "xuanhuan":
                filter = eq("rootcatlog", "玄幻");
                break;
            case "wuxia":
                filter = eq("rootcatlog", "武侠");
                break;
            case "xianxia":
                filter = eq("rootcatlog", "仙侠");
                break;
            case "dushi":
                filter = eq("rootcatlog", "都市");
                break;
            case "yanqing":
                filter = eq("rootcatlog", "言情");
                break;
            case "lishi":
                filter = eq("rootcatlog", "历史");
                break;
            case "junshi":
                filter = eq("rootcatlog", "军事");
                break;
            case "youxi":
                filter = eq("rootcatlog", "游戏");
                break;
            case "tiyu":
                filter = eq("rootcatlog", "体育");
                break;
            case "kehuan":
                filter = eq("rootcatlog", "科幻");
                break;
            case "lingyi":
                filter = eq("rootcatlog", "灵异");
                break;
        }

        return filter;
    }
}
